package com.wanxing;

import com.wanxing.main.MainActivity;
import com.zhny.wxginger.R;
import com.util.HttpUtils;
import com.util.Info;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class LoginActivity extends Activity {

	private EditText e_id;
	private EditText e_pw;
	private Button b_login;
	private CheckBox e_re;

	private String user;
	private String password;

	public static SharedPreferences preferences;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);
		LinkInternet(isOpenNetwork());

		init();

		if (preferences.getBoolean("ISCHECK", false)) {
			// ����Ĭ���Ǽ�¼��ã״̬
			e_re.setChecked(true);
			e_id.setText(preferences.getString("USER_NAME", ""));
			e_pw.setText(preferences.getString("PASSWORD", ""));
			// �ж��Զ���¼��ѡ��״̬
			/*
			 * if(preferences.getBoolean("AUTO_ISCHECK", false)){
			 * 
			 * //����Ĭ�����Զ���¼״̬ auto_login.setChecked(true); Intent intent=new
			 * Intent(Login.this,MainActivity.class);
			 * Login.this.startActivity(intent); Login.this.finish(); }
			 */
		}

		b_login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				user = e_id.getText().toString().trim();
				password = e_pw.getText().toString().trim();
				String URL = Info.path_login + "?user=" + user + "&password="
						+ password;
				System.out.println(URL);
				System.out.println("########################");

				new MyAsyncTask().execute(URL);
			}
		});
		// ��ס����
		e_re.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (e_re.isChecked()) {

					System.out.println("��ס����ѡ��");
					preferences.edit().putBoolean("ISCHECK", true).commit();
				} else {
					System.out.println("��ס����δ��ѡ��");
					preferences.edit().putBoolean("ISCHECK", false).commit();
				}
			}
		});
	}

	public void init() {
		preferences = this.getSharedPreferences("userInfo",
				Context.MODE_PRIVATE);
		e_id = (EditText) findViewById(R.id.l_id);
		e_pw = (EditText) findViewById(R.id.l_pw);
		b_login = (Button) findViewById(R.id.l_login);
		e_re = (CheckBox) findViewById(R.id.l_re);
	}

	// ��¼ �첽 Login MyAsyncTask
	public class MyAsyncTask extends AsyncTask<String, Void, String> {
		@Override
		protected String doInBackground(String... arg0) {
			// TODO Auto-generated method stub
			return HttpUtils.sendPostMethod(arg0[0], "utf-8");
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result.equals("success")) {
				if (e_re.isChecked()) {
					Editor editor = preferences.edit();
					editor.putString("USER_NAME", user);
					editor.putString("PASSWORD", password);
					editor.commit();
				}
				Intent intent = new Intent(LoginActivity.this,
						MainActivity.class);
				startActivity(intent);
				// finish();
				LoginActivity.this.finish();
			} else if (result.equals("fail")) {
				Toast.makeText(LoginActivity.this, "�˺�����������������룡",
						Toast.LENGTH_SHORT).show();
			} else {
				if (!isOpenNetwork())
					Toast.makeText(LoginActivity.this, "������������", 1).show();
			}
			
		}

	}

	// �ж��Ƿ���������
	private boolean isOpenNetwork() {
		ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connManager.getActiveNetworkInfo() != null) {
			return connManager.getActiveNetworkInfo().isAvailable();
		}

		return false;
	}

	// �����ж�
	public void LinkInternet(boolean OpenNetwork) {
		if (!OpenNetwork) {
			AlertDialog.Builder builder = new AlertDialog.Builder(
					LoginActivity.this);
			builder.setTitle("û�п��õ�����").setMessage("�Ƿ�������������?");

			builder.setPositiveButton("��",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Intent intent = null;

							try {
								String sdkVersion = android.os.Build.VERSION.SDK;
								if (Integer.valueOf(sdkVersion) > 10) {
									intent = new Intent(
											android.provider.Settings.ACTION_SETTINGS);
								} else {
									intent = new Intent();
									ComponentName comp = new ComponentName(
											"com.android.settings",
											"com.android.settings.WirelessSettings");
									intent.setComponent(comp);
									intent.setAction("android.intent.action.VIEW");
								}
								LoginActivity.this.startActivity(intent);
							} catch (Exception e) {
								System.out
										.println("open network settings failed, please check...");
								e.printStackTrace();
							}
						}
					})
					.setNegativeButton("��",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.cancel();
									// DengluAcitivity.this.finish();
								}
							}).show();
		}

	}
}
